/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.c1202m.connection;

import com.c1202m.gui.CustomMessageDialog;
import com.c1202m.hibernate.entities.Server;
import java.io.File;
import java.io.PrintWriter;

/**
 *
 * @author CodeBlue
 */
// This class is for generating hibernate config file after the program connects to server successfully
public class HibernateConfigGenerator {

    public HibernateConfigGenerator(Server server) {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
        xml += "<!DOCTYPE hibernate-configuration PUBLIC \"-//Hibernate/Hibernate Configuration DTD 3.0//EN\" \"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd\">";
        xml += "<hibernate-configuration>";
        xml += "<session-factory>";
        xml += "<property name=\"hibernate.connection.driver_class\">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>";
        xml += "<property name=\"hibernate.connection.url\">jdbc:sqlserver://" + server.getServerName() + ":" + server.getPort() + ";databaseName=" + server.getDatabaseName() + "</property>";
        xml += "<property name=\"hibernate.connection.username\">" + server.getUserName() + "</property>";
        xml += "<property name=\"hibernate.connection.password\">" + server.getPassword() + "</property>";
        xml += "<property name=\"hibernate.dialect\">org.hibernate.dialect.SQLServerDialect</property>";
        xml += "<property name=\"hibernate.current_session_context_class\">thread</property>";
        xml += "<property name=\"hibernate.query.factory_class\">org.hibernate.hql.ast.ASTQueryTranslatorFactory</property>";
        xml += "<property name=\"hibernate.hbm2ddl.auto\">update</property>";
        xml += "<mapping resource=\"com/c1202m/hibernate/entities/QuanTri.hbm.xml\"/>";
        xml += "<mapping resource=\"com/c1202m/hibernate/entities/TaiKhoan.hbm.xml\"/>";
        xml += "<mapping resource=\"com/c1202m/hibernate/entities/RutTien.hbm.xml\"/>";
        xml += "<mapping resource=\"com/c1202m/hibernate/entities/GuiTien.hbm.xml\"/>";
        xml += "<mapping resource=\"com/c1202m/hibernate/entities/ChuyenKhoan.hbm.xml\"/>";
        xml += "</session-factory>";
        xml += "</hibernate-configuration>";

        try {
            // create cfg.xml file
            PrintWriter pw;
            pw = new PrintWriter(new File("cfg.xml"));
            // write to file
            pw.write(xml);
            pw.close();
        } catch (Exception e) {
            new CustomMessageDialog(null, true, "Error!", "An error occured while writing to file", CustomMessageDialog.MESSAGE);
        }
    }
}
